import axios from 'axios'
import Config from '@/settings'
import { Toast } from "vant";
// 创建axios实例
const service = axios.create()
// service.defaults.baseURL = 'http://192.168.1.39:9184'
// service.defaults.baseURL = 'http://192.168.1.74:9184'
// service.defaults.baseURL = 'http://112.14.45.72:9184'
service.defaults.timeout = Config.timeout
// request拦截器
service.interceptors.request.use(
    config => {
        config.headers['sid'] = window.localStorage.getItem('TOKEN_t')
        config.headers['Content-Type'] = 'application/json'
        return config
    },
    error => {

        Promise.reject(error)
    }
)

// response 拦截器
service.interceptors.response.use(
    response => {
        const code = response.status
        if (code < 200 || code > 300) {
            Toast({
                message: response.message,
                duration: 1500
            })

            return Promise.reject('error')
        } else {
            return response.data
        }
    },
    error => {
        let code = 0
        try {
            code = error.response.data.status
        } catch (e) {
            if (error.toString().indexOf('Error: timeout') !== -1) {
                Toast({
                    message: 'network request timed out',
                    duration: 1500
                })
                return Promise.reject(error)
            }
        }
        if (code) {
            if (code === 401) {
                Toast({
                    message: 'Request timed out, please login again',
                    duration: 1500
                })
            }
            else {
                const errorMsg = error.response.data.message
                if (errorMsg !== undefined) {
                    Toast({
                        message: errorMsg,
                        duration: 1500
                    })
                }
            }
        } else {
            Toast({
                message: 'The interface request failed',
                duration: 1500
            })
            // Notification.error({
            //     title: '接口请求失败',
            //     duration: 5000
            // })
        }
        return Promise.reject(error)
    }
)
export default service